var theUser;
$(function(){
	theUser=$.cookie("userId");
	console.log(theUser);
});

$(function(){
	$("#chooseType li").click(function(event){
		var tar=$(event.target);
		$(tar[0]).css({borderBottom:"2px solid rgb(6,193,174)",color:"rgb(6,193,174)"});
		var brother=tar.siblings();
        brother.each(function(){
        	$(this).css({borderBottom:"none",color:"black"});
        });
	});
	$("#chooseType li").click(function(){
		var x=$(this).html().replace(/\s/g,"");
		if(x=="全部"){
			allSkill()
		}else{
			typeSkill(x);
		}
	});
});

$(function(){
	$("#chooseType li:first").click();
});

$(function(){
	var remChoose=$("#remChoose");
	var remType=$("#remType");
	$("#RMB").click(function(){
		remType.html('<div class="ui-input-text ui-body-inherit ui-corner-all ui-mini ui-shadow-inset"><input type="number" placeholder="请输入金额" data-mini="true" id="remSet"></div>');
	});
	$("#skillChange").click(function(){
		remType.html('<textarea rows="5" cols="20" placeholder="作为交换，您可以为对方提供的技能" class="ui-input-text ui-shadow-inset ui-body-inherit ui-corner-all ui-textinput-autogrow" style="height: 52px;" id="remSet"></textarea>');
	});
});

$(function(){
	$("#findSkill").on("swiperight",function(){
		$("#controlPanel").panel("open");
	});
});

//全部信息
function allSkill(){
	$.ajax({
		type:"GET",
		url:"http://123.207.114.241/fishCow/php/selectSkillFind.php",
		dataType:"json",
		success:function(x,y,z){
			var n;
			var list=$("#messageList ul");
			list.empty();
			for(n=0;n<x.length;n++){
				list.append(
					'<li><a href="#details" onclick="detailGo(event)"><div>标题:'
					+x[n].nenufish_skillFind_title
					+'</div><div>发布时间:'
					+x[n].nenufish_skillFind_datatime
					+'</div><div>摘要:'
					+x[n].nenufish_skillFind_content
					+'</div><div class="transparent">'
					+x[n].nenufish_skillFind_id
					+'</div></a></li>'
				)
			}
		},
		error:function(jqXHR){
		    console.log("发生错误"+jqXHR.status);
		}
	});
}

//选择技能
function typeSkill(skillType){
	$.ajax({
		type:"GET",
		url:"http://123.207.114.241/fishCow/php/selectSkillFind_type.php?type="+skillType,
		dataType:"json",
		success:function(x,y,z){
			var list=$("#messageList ul");
			list.empty();
			if(!x){
				list.append('<li class="nomessage">暂无信息>_<....</li>')
			}else{
				var n;
				for(n=0;n<x.length;n++){
					list.append(
						'<li><a href="#details" onclick="detailGo(event)"><div>标题:'
						+x[n].nenufish_skillFind_title
						+'</div><div>发布时间:'
						+x[n].nenufish_skillFind_datatime
						+'</div><div class="text_over">摘要:'
						+x[n].nenufish_skillFind_content
						+'</div><div class="transparent">'
						+x[n].nenufish_skillFind_id
						+'</div></a></li>'
					)
				}
			}
		},
		error:function(jqXHR){
		    console.log("发生错误"+jqXHR.status);
		}
	});
}

function detailGo(event){
	var target=event.target;
	var parenta=$(target).parents("li");
	var theId=parenta.find(".transparent").html();
	if(theId!=">暂无信息>_<...."){
		$.ajax({
			type:"GET",
			url:"http://123.207.114.241/fishCow/php/selectSkillfind_xiangxi.php?id="+theId,
			dataType:"json",
			success:function(x,y,z){
				var detailed=$("#detailed");
				detailed.children("#theTitle").html("标题:"+x.nenufish_skillFind_title);
				detailed.children("#releaseTime").html("发布时间:"+x.nenufish_skillFind_datatime);
				detailed.children("#skillType").html("技能分类:"+x.nenufish_skillFind_type);
				detailed.children("#people").html("发布人:"+x.nenufish_skillFind_editor);
				if(!x.nenufish_skillFind_pay){
					detailed.children("#theRem").html("报酬:"+x.nenufish_skillFind_payType);
				}else{
					detailed.children("#theRem").html("报酬:"+x.nenufish_skillFind_pay+"元");
				}
				detailed.children("#requirement").html("<span>技能要求:</span><span>"+x.nenufish_skillFind_content+"</span>");
				detailed.children("#nnedTime").html("时间限制:"+x.nenufish_skillFind_time+"天");
				detailed.children("#contactQQ").html("QQ:"+x.nenufish_skillFind_qq);
				detailed.children("#contactPhone").html("电话:"+x.nenufish_skillFind_phone);
			},
			error:function(jqXHR){
			    console.log("发生错误"+jqXHR.status);
			}
		});
	}
}

//发布需求
$(function(){
	$("#releaseGo").click(function(){
		var title=$("#title").val();
		var skilltype=$("#skillClass input:checked").val();
		var content=$("#skillSet").val();
		var payType=$("#remuneration input:checked").val();
		var pay=$("#remSet").val();
		var time=$("#limitTime").val();
		var phone=$("#telephone").val();
		var qq=$("#QQ").val();

		var a=[];
		var titleWar=$("#titleWar");
		var skillClass=$("#skillClass").find("legend");
		var skillSetWar=$("#skillSetWar");
		var remChoose=$("#remChoose");
		var limitWar=$("#limitWar");
		var phoneWar=$("#phoneWar");
		var qqWar=$("#qqWar");

		if(title==""){
			titleWar.html("标题:不能为空");
			titleWar.addClass("warning");
			a[0]=false;
		}else{
			titleWar.html("标题:");
			titleWar.removeClass("warning");
			a[0]=true;
		};
		if(!skilltype){
			skillClass.html("技能分类:请选择一种技能");
			skillClass.addClass("warning");
			a[1]=false;
		}else{
			skillClass.html("技能分类:");
			skillClass.removeClass("warning");
			a[1]=true;
		};
		if(content==""){
			skillSetWar.html("需要技能设定:请填写");
			skillSetWar.addClass("warning");
			a[2]=false;
		}else{
			skillSetWar.html("需要技能设定:");
			skillSetWar.removeClass("warning");
			a[2]=true;
		};
		if(pay==""){
			remChoose.html("报酬设定:请填写");
			remChoose.addClass("warning");
			a[3]=false;
		}else{
			remChoose.html("报酬设定:");
			remChoose.removeClass("warning");
			a[3]=true;
		};
		if(time==""){
			limitWar.html("时间限制:请填写天数");
			limitWar.addClass("warning");
			a[4]=false;
		}else{
			limitWar.html("时间限制:(天)");
			limitWar.removeClass("warning");
			a[4]=true;
		};
		if(phone==""){
			phoneWar.html("电话:请填写");
			phoneWar.addClass("warning");
			a[5]=false;
		}else{
			phoneWar.html("电话:");
			phoneWar.removeClass("warning");
			a[5]=true;
		};
		if(qq==""){
			qqWar.html("QQ:请填写");
			qqWar.addClass("warning");
			a[6]=false;
		}else{
			qqWar.html("QQ:");
			qqWar.removeClass("warning");
			a[6]=true;
		};

		if(a[0]&&a[1]&&a[2]&&a[3]&&a[4]&&a[5]&&a[6]){
			needGo(title,skilltype,content,payType,pay,time,phone,qq);
		}else{
			$.mobile.silentScroll(0);
		}
	});
});

function needGo(a,b,c,d,e,f,g,h){
	$.ajax({
		type:"POST",
		url:"http://123.207.114.241/fishCow/php/AddSkillFind.php",
		dataType:"json",
		data:{
			title:a,
			skilltype:b,
			content:c,
			payType:d,
			pay:e,
			time:f,
			phone:g,
			qq:h,
			userid:theUser
		},
		success:function(x,y,z){
			if(x=="success"){
				alert("发布成功");
			}else{
				alert("发布失败");
			}
		},
		error:function(jqXHR){
		    alert("发生错误:"+jqXHR.status);
		}
	});
}